package com.scut.campus.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.scut.campus.context.BaseContext;
import com.scut.campus.dto.LostDto;
import com.scut.campus.dto.LostUpdateDto;
import com.scut.campus.dto.PageDTO;
import com.scut.campus.entity.Lost;
import com.scut.campus.query.LostQuery;
import com.scut.campus.query.base.PageQuery;
import com.scut.campus.response.Result;
import com.scut.campus.service.LostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "失物招领功能")
@Slf4j
@RequestMapping("/lost")
public class LostController {
    @Autowired
    private LostService lostService;

    @PostMapping("/insert")
    @Operation(summary = "上传失物")
    public Result insertLost(@RequestBody LostDto lostDto) {
        lostService.insertLost(lostDto);
        return Result.success();
    }

    @PostMapping("/update")
    @Operation(summary = "编辑失物描述")
    public Result updateLost(@RequestBody LostUpdateDto lostDto) {

        lostService.updateLost(lostDto);
        return Result.success();
    }

    @DeleteMapping("/delete/{lostId}")
    @Operation(summary = "删除我的失物")
    public Result deleteLost(@PathVariable Long lostId) {
        LambdaQueryWrapper<Lost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Lost::getLostId, lostId)
                .eq(Lost::getUserId, BaseContext.getCurrentId());
        lostService.remove(lambdaQueryWrapper);

        return Result.success();
    }

    @PostMapping("/queryMyLost")
    @Operation(summary = "分页查询我的失物")
    public Result<PageDTO<Lost>> queryMyLost(@RequestBody PageQuery pageQuery) {
        PageDTO<Lost> pageResult = lostService.queryMyLost(pageQuery);
        return Result.success(pageResult);
    }

    @PostMapping("/list")
    @Operation(summary = "首页展示失物")
    public Result<PageDTO<Lost>> listLost(@RequestBody LostQuery lostQuery) {
        PageDTO<Lost> pageResult = lostService.listLost(lostQuery);
        return Result.success(pageResult);
    }

    @GetMapping("/detail/{lostId}")
    @Operation(summary = "获取单个失物详情")
    public Result<Lost> getOneById(@PathVariable("lostId") Long lostId) {
        Lost lost = lostService.getOneById(lostId);
        return Result.success(lost);
    }

}
